/*
 * @Author: SamZhang samzhang@apjcorp.com
 * @Date: 2022-12-02 10:14:44
 * @LastEditors: SamZhang samzhang@apjcorp.com
 * @LastEditTime: 2023-02-16 15:03:21
 * @FilePath: /circulation-web/src/components/AnalyticsServices.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import EditableConfigList, {
  EditableConfigListStateProps,
  EditableConfigListDispatchProps,
  EditableConfigListOwnProps,
} from "./EditableConfigList";
import { connect } from "react-redux";
import ActionCreator from "../actions";
import { AnalyticsServicesData, AnalyticsServiceData } from "../interfaces";
import ServiceEditForm from "./ServiceEditForm";
import NeighborhoodAnalyticsForm from "./NeighborhoodAnalyticsForm";

const applocation_root = localStorage.getItem("MOBEBR_CONFIG_APPLICATION_ROOT");

/** Right panel for analytics services on the system configuration page.
    Shows a list of current analytics services and allows creating a new
    service or editing or deleting an existing service. */
export class AnalyticsServices extends EditableConfigList<
  AnalyticsServicesData,
  AnalyticsServiceData
> {
  EditForm = ServiceEditForm;
  ExtraFormSection = NeighborhoodAnalyticsForm;
  extraFormKey = "location_source";
  listDataKey = "analytics_services";
  itemTypeName = "analytics service";
  urlBase = applocation_root + "/admin/web/config/analytics/";
  identifierKey = "id";
  labelKey = "protocol";

  label(item): string {
    for (const protocol of this.props.data.protocols) {
      if (protocol.name === item.protocol) {
        return protocol.label;
      }
    }
    return item.protocol;
  }
}

function mapStateToProps(state, ownProps) {
  const data = Object.assign(
    {},
    (state.editor.analyticsServices && state.editor.analyticsServices.data) ||
      {}
  );
  if (state.editor.libraries && state.editor.libraries.data) {
    data.allLibraries = state.editor.libraries.data.libraries;
  }
  // fetchError = an error involving loading the list of analytics services; formError = an error upon submission of the
  // create/edit form.
  return {
    data: data,
    responseBody:
      state.editor.analyticsServices &&
      state.editor.analyticsServices.successMessage,
    fetchError: state.editor.analyticsServices.fetchError,
    formError: state.editor.analyticsServices.formError,
    isFetching:
      state.editor.analyticsServices.isFetching ||
      state.editor.analyticsServices.isEditing,
  };
}

function mapDispatchToProps(dispatch, ownProps) {
  const actions = new ActionCreator(null, ownProps.csrfToken);
  return {
    fetchData: () => dispatch(actions.fetchAnalyticsServices()),
    editItem: (data: FormData) => dispatch(actions.editAnalyticsService(data)),
    deleteItem: (identifier: string | number) =>
      dispatch(actions.deleteAnalyticsService(identifier)),
  };
}

const ConnectedAnalyticsServices = connect<
  EditableConfigListStateProps<AnalyticsServicesData>,
  EditableConfigListDispatchProps<AnalyticsServicesData>,
  EditableConfigListOwnProps
>(
  mapStateToProps,
  mapDispatchToProps
)(AnalyticsServices);

export default ConnectedAnalyticsServices;
